package com.share51.novel.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.share51.novel.common.StatusConstant;
import com.share51.novel.dao.ChapterMapper;
import com.share51.novel.dao.ContentMapper;
import com.share51.novel.dao.NovelMapper;
import com.share51.novel.entity.Chapter;
import com.share51.novel.entity.Content;
import com.share51.novel.entity.Novel;
import com.share51.novel.service.NovelService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 小说处理业务
 *
 * @author gb
 */
@Slf4j
@Service("novelService")
public class NovelServiceImpl implements NovelService {

    @Resource
    private NovelMapper novelMapper;

    @Resource
    private ChapterMapper chapterMapper;

    @Resource
    private ContentMapper contentMapper;

    @Override
    public Integer save(Novel novel) {
        return novelMapper.insert(novel);
    }

    @Override
    public Integer update(Novel novel) {
        return novelMapper.updateNovel(novel);
    }

    @Override
    public Novel getById(Integer id) {
        Novel novel = novelMapper.selectById(id);
        if (novel==null){
            throw new RuntimeException("参数错误，传入的小说编号不存在，id:" + id);
        }
        return novel;
    }

    @Override
    public void saveChapter(List<Chapter> chapterList, Integer novelId, String hostName) {
        log.debug("存储小说章节数据，小说编号：{}，章节总数：{}", novelId, chapterList.size());
        chapterList = chapterList
                .stream()
                .peek(chapter -> {
                    chapter.setNovelId(novelId);
                    chapter.setStatus(StatusConstant.NovelChapterStatus.WAIT_CRAWL);
                    chapter.setUrl(chapter.getUrl().replace(hostName, ""));
                })
                .collect(Collectors.toList());
        if (chapterList!=null&&!chapterList.isEmpty()){
            chapterMapper.saveAll(chapterList);
        }
    }

    @Override
    public List<Chapter> getAllChapterByStatus(Integer status) {
        return chapterMapper.getTop5000ChapterByStatus(status);
    }

    @Override
    public List<String> getAllChapterUrlAndSite() {
        return chapterMapper
                .getAllChapterUrlAndSite()
                .stream()
                .map(chapter -> chapter.getSite()+""+chapter.getUrl())
                .collect(Collectors.toList());
    }

    @Override
    public List<String> getChapterByLimit(Integer novelId, int chapterLimit) {
        return chapterMapper.getChapterUrlByLimit(novelId,chapterLimit);
    }

    @Override
    public Integer saveContent(Content content) {
        return contentMapper.saveContent(content);
    }

    @Override
    public void updateChapter(Chapter chapter) {
        chapterMapper.updateChapter(chapter);
    }

    @Override
    public List<Novel> getAllNovelBySeriesStatus(Integer status) {
        return novelMapper.getAllNovelBySeriesStatus(status);
    }

    @Override
    public void editNovelSeriesStatus(Novel novel) {
        novelMapper.editNovelSeriesStatus(novel);
    }

    @Override
    public void editNovelStatusByStatus(Novel novel, int novelWaitPublish) {
        novelMapper.editNovelStatusByStatus(novel.getId(),novel.getStatus(),novelWaitPublish);
    }

    @Override
    public Novel getNovelInfoByDownload(Integer novelId) {
        return novelMapper.getNovelNameAndStatusById(novelId);
    }

    @Override
    public void updateNovelStatusAndNotDel(Integer novelId, int statusOk) {
        novelMapper.updateNovelStatus(novelId,statusOk,StatusConstant.STATUS_DEL);
    }

    @Override
    public List<Novel> getNewest(int total, Integer category) {
        QueryWrapper<Novel> queryWrapper= new QueryWrapper<>();
        if (category!=null){
            queryWrapper.eq("category",category);
        }
        Page<Novel> page = new Page<>(1,total);
        page.addOrder(OrderItem.descs("id","update_time"));
        Page<Novel> novelPage = novelMapper.selectPage(page, queryWrapper);
        return novelPage.getRecords();
    }
}
